package com.ygr.anime.service.impl;

import cn.dev33.satoken.model.wrapperInfo.SaDisableWrapperInfo;
import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ygr.anime.domain.Permission;
import com.ygr.anime.domain.Role;
import com.ygr.anime.domain.RolePermission;
import com.ygr.anime.domain.UserRole;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@AllArgsConstructor
public class StpInterfaceImpl implements StpInterface {


    private final BaseMapper<RolePermission> rolePermissionMapper;
    private final BaseMapper<Permission> permissionMapper;
    private final BaseMapper<UserRole> userRoleMapper;
    private final BaseMapper<Role> roleMapper;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        List<String> permissions = new ArrayList<>();
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(
                new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, loginId)
        );
        for (RolePermission rolePermission : rolePermissions) {
            Permission permission = permissionMapper.selectById(rolePermission.getPermissionId());
            permissions.add(permission.getName());

        }
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roles = new ArrayList<>();
        List<UserRole> userRoles = userRoleMapper.selectList(
                new LambdaQueryWrapper<UserRole>().eq(
                        UserRole::getUserId, loginId
                )
        );
        for (UserRole userRole : userRoles) {
            Role role = roleMapper.selectById(userRole.getRoleId());
            roles.add(role.getName());
        }
        return roles;
    }
}
